package gui.frame;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Canvas;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Label;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class FrameToeTest extends Frame {

	TextField rows = new TextField("3");
	TextField cols = new TextField("3");

	public FrameToeTest() {
		this.setTitle("Frame ToeTest");

		Panel p = new Panel();
		p.setLayout(new GridLayout(2, 2));
		p.add(new Label("Rows", Label.CENTER));
		p.add(rows);
		p.add(new Label("Columns", Label.CENTER));
		p.add(cols);

		this.add(p, BorderLayout.NORTH);

		Button btn = new Button("Go");
		btn.addActionListener(new BL());

		this.add(btn, BorderLayout.SOUTH);
	}

	static final int BLANK = 0;
	static final int XX = 1;
	static final int OO = 2;

	class ToeDialog extends Dialog {
		int turn = XX;

		ToeDialog(int w, int h) {
			super(FrameToeTest.this, "The game itself", false);
			this.setLayout(new GridLayout(w, h));
			for (int i = 0; i < w * h; i++)
				this.add(new ToeButton());
			this.setSize(w * 40, h * 50);
			this.addWindowListener(new WindowAdapter() {
				public void windowClosing(WindowEvent e) {
					dispose();
				}
			});
		}

		class ToeButton extends Canvas {
			int state = BLANK;

			ToeButton() {
				this.addMouseListener(new ML());
			}

			public void paint(Graphics g) {
				int x1 = 0, x2 = this.getSize().width - 1;
				int y1 = 0, y2 = this.getSize().height -1;

				g.drawRect(x1, y1, x2, y2);

				x1 = x2 / 4;
				y1 = y2 / 4;
				int wide = x2 / 2;
				int high = y2 / 2;

				if (state == OO) {
					g.drawOval(x1, y1, x1 + wide / 2, y1 + high);
				}

				if (state == XX) {
					g.drawLine(x1, y1, x1 + wide, y1 + high);
					g.drawLine(x1, y1 + high, x1 + wide, y1);
				}

			}

			class ML extends MouseAdapter {
				public void mousePressed(MouseEvent e) {
					if (state == BLANK) {
						state = turn;
						turn = (turn == XX ? OO : XX);
					} else {
						state = (state == XX ? OO : XX);
					}
				}
			}//ML
		}//ToeButton
	}//ToeDialog
	
	class BL implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			Dialog dialog=new ToeDialog(Integer.parseInt(rows.getText()),Integer.parseInt(cols.getText()));
			dialog.show();
		}
		
	}//BL
	
	public static void main(String[]args){
		Frame frame=new FrameToeTest();
		frame.addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent e){
				System.exit(0);
			}
		});
		frame.setSize(200, 100);
		frame.setVisible(true);
	}
}
